import axios from 'axios';
import router from '../router'; // 假设你的路由器实例被导入为 router

// 创建 Axios 实例
const axiosInstance = axios.create({
  // baseURL: 'http://124.220.40.115:82',
  timeout: 5000,
});

// 请求拦截器
axiosInstance.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('token');

    if (token) {

      if(config.url!='https://lakk.windbless.top/pairs/help.php')
      config.headers.Authorization = token;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

axiosInstance.interceptors.response.use(
  (response) => {
    if (response.data.code === 401) {
      router.push('/login');
    }
    return response.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 定义接口
const api = {
  login: (data) => axiosInstance.post('/prod-api/login', data),
  getinfo: () => axiosInstance.get('/prod-api/lakk/UserAssociation/who_am_i'),
  putinfo: (data) => axiosInstance.put('/prod-api/lakk/UserAssociation', data),
  addinfo: (data) => axiosInstance.post('/prod-api/lakk/UserAssociation/addthis', data),
  getquestion1: (params) => axiosInstance.get('/prod-api/lakk/UserProgress/list', { params }),
  produceProgressThisDay: () => axiosInstance.get('/prod-api/lakk/UserProgress/produceProgressThisDay'),
  getonelist:(params) => axiosInstance.get('/prod-api/lakk/QuestionBank/list', { params }),
  getallcoins:()=>axiosInstance.get('/prod-api/lakk/UserCoinRecord/list'),
  getTellStory:()=>axiosInstance.get('/prod-api/lakk/UserProgress/hasTellStory'),
  updatefile:(data)=>axiosInstance.post('prod-api/common/upload',data),
  putUserProgress:(data)=>axiosInstance.put('prod-api/lakk/UserProgress',data),
  putallroom:()=>axiosInstance.get('prod-api/memory/list'),
  createroom:(data)=>axiosInstance.post('prod-api/memory/create',data),
  addroom:(data)=>axiosInstance.post('prod-api/memory/add',data),
  getroom:(params)=>axiosInstance.get('prod-api/memory/get', { params }),
  putcion:(data)=>axiosInstance.post('prod-api/lakk/UserCoinRecord',data),
  addtime:(data)=>axiosInstance.post('prod-api/lakk/UserPlaytimeRecord',data),
  gettime:(params)=>axiosInstance.get('prod-api/lakk/UserPlaytimeRecord/list',{ params }),
  getkey:(params)=>axiosInstance.get('https://lakk.windbless.top/pairs/help.php',{ params }),
  getallkey:()=>axiosInstance.get("prod-api/memory/list")
};

export default api;